<?php


namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\News;
use App\Models\Project;
use Illuminate\Http\Request;

class NewsController extends BaseController
{
    public $model = News::class;


    public function index(Request $request) {
        $input = $request->input();
        $search = ['on_status' => -1, 'start_at' => '', 'end_at' => ''];
        $name = $request->input('name');
        $project_id = $request->input('project_id');
        $news_type = $request->input('news_type');
        $news_tag = $request->input('news_tag');
        $company_type = $request->input('company_type');
        $list = News::withCertain('project', ['name'])->apply($search, $input)
            ->when($name, function ($q) use ($name) {
                $q->where('name', 'like', '%' . $name . '%');
            })
            ->when($news_type, function ($q) use ($news_type) {
                $q->where('news_type', $news_type);
            })
            ->when($project_id, function ($q) use ($project_id) {
                $q->where('project_id', $project_id);
            })
            ->when($company_type, function ($q) use ($company_type) {
                $q->where('company_type', $company_type);
            })
            ->when($news_tag, function ($q) use ($news_tag) {
                if ($news_tag == 61)
                    $q->where('is_rec_read', 1);
                else if($news_tag == 62)
                    $q->where('is_focus', 1);
                else if($news_tag == 63)
                    $q->where('join_coupon', 1);
                else if($news_tag == 64)
                    $q->where('homepage_1', 1);
                else if($news_tag == 65)
                    $q->where('homepage_2', 1);
                else if($news_tag == 66)
                    $q->where('homepage_3', 1);
                else if($news_tag == 67)
                    $q->where('mini_homepage', 1);

            })
            ->orderBy("sort", "desc")->orderBy("pv", "desc")->orderBy("id", "desc")
            ->paginate(per_page());
        foreach ($list as $item) {
            $item->append(['news_type_show', 'news_tag_show']);
        }
        return $this->paginate($list);
    }


    /**
     * 保存|修改
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request) {
        $input = $request->input();
        $id = \request("id");
        $news_tag = $input['news_tag'] ?? null;
        $arr_news_tag = json_decode($news_tag, true);
        $input['is_rec_read'] = in_array('61', $arr_news_tag) ? 1 : 0;
        $input['is_focus'] = in_array('62', $arr_news_tag) ? 1 : 0;
        $input['join_coupon'] = in_array('63', $arr_news_tag) ? 1 : 0;
        $input['homepage_1'] = in_array('64', $arr_news_tag) ? 1 : 0;
        $input['homepage_2'] = in_array('65', $arr_news_tag) ? 1 : 0;
        $input['homepage_3'] = in_array('66', $arr_news_tag) ? 1 : 0;
        $input['mini_homepage'] = in_array('67', $arr_news_tag) ? 1 : 0;

        $project = Project::find($input['project_id']);
        if (!$project)
            return $this->failed('项目不存在');
        $input['parent_category_id'] = $project->parent_category_id;

        if ($id) {
            $model = News::find($id);
            $model->update($input);
        } else {
            $info = News::create($input);
            $id = $info->id;
        }
        return $this->success($id);
    }

    public function destroy(Request $request) {
        $this->validate($request, [
            'ids' => 'required'
        ]);
        $ids = $request->input('ids');
        $arr_id = explode(",", $ids);
        foreach ($arr_id as $id) {
            $model = News::find ($id);
            $model->delete();
        }
        return $this->success([]);
    }

}

